home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr40 / x1j4pk96.zip / X1JCONV.C < prev    next >
Text File  |  1994-07-12  |  14KB  |  363 lines

  1. /*
  2. Program: X1JCONV.C 1.1 (c) 1994
  3. Date: 02-JUL-94
  4. Updates: 07-JUL-94
  5.          Fix search routine to allow 'Net/Rom' on Iproute line.
  6. Author: Ken Chamberlain, N8PTT.AMPR.ORG [44.70.179.1]
  7.                  Packet: N8PTT@W8FT.#NWOH.OH.USA.NOAM
  8.                  Internet: N8PTT@CRIS.COM
  9. Function: Converts the 'record'ed output of an X1J display of
  10.    of the following commands to a file suitable for loading back to X1J.
  11.    ipaddress
  12.    iproutes
  13.    arp
  14.    routes
  15.    mode      <- only 1 line of output (it SHOULD fit)
  16.    parm      <- due to size of parm each entry is on a seperate line
  17.  
  18. Comments:
  19.    This program is use for Amateur Radio only.
  20.    It may be freely copied for such use provided its source is acknowledged.
  21.    It may not be used for commercial purposes.
  22.    Written using X1J Version 2 displays, untested w/ other version of X1J.
  23.    No Warranty is Implied or Expressed by the Author and
  24.      accepts NO responsible for anything this program may
  25.      destroy on purpose or by accident.
  26.  
  27.    The mode & parm routines depend in finding the word 'mode' and
  28.      and the word 'parm' in the capture file so I know the following
  29.      rcds in the file contain the information. I'm working on a better
  30.      way to do this and will probably be in version 1.2
  31.  
  32. */
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. char author[] = "X1JCONV 1.1 Written by Ken Chamberlain N8PTT (c) 1994";
  38. FILE *f1,*f2;                      /* input & output FILE(s)        */
  39. #define MAXLEN 80                  /* max input rcd length (I hope) */
  40. void isiprec(void);                /* build the IP rcd              */
  41. void isarp(void);                  /* build the ARP rcd             */
  42. void isroute(void);                /* build the ROUTES rcd          */
  43. void ismyip(void);                 /* build the IPADDRESS rcd       */
  44. void ismode(void);                 /* build the MODE rcd            */
  45. void isparm(void);                 /* build the PARM rcd            */
  46. void moveparm(void);               /* Move the data for PARM        */
  47. void writeme(void);                /* output writer                 */
  48. int nxtnblk(int);                  /* find next non-blank byte      */
  49. int nxtblk(int);                   /* find next blank byte          */
  50. char buf[80],outbuf[70],oldbuf[80];
  51. int DEBUG;
  52. int outcnt,incnt,cnt;
  53. int cntr = 1;
  54. int cntr2 = 0;
  55. static isiport = 1;
  56. char work[5];
  57. static int howmanyin,howmanyout;
  58. char *isip;
  59. char *sa,*ptr;
  60. void main(int argc,char *argv[])
  61. {
  62.  if(argc < 3)
  63.   {
  64.    printf("X1JCONV 1.2 converts a 'record'ed capture of X1J display output\n");
  65.    printf("        to a file suitable for re-loading back to the X1J.\n");
  66.    printf("        supports output of: ipaddr,ip,arp,r,mode, & parms\n");
  67.    printf("        2 args need: x1jconv infile outfile\n");
  68.    exit(1);
  69.   }
  70.  printf("Input File: %s, Output File: %s\n",argv[1],argv[2]);
  71.  if((f1 = fopen(argv[1],"r")) == NULL)
  72.    {
  73.     fprintf(stderr,"Cannot open input file.\n");
  74.     exit(1);
  75.    }
  76.  if((f2 = fopen(argv[2],"w")) == NULL)
  77.    {
  78.     fprintf(stderr,"Cannot open output file.\n");
  79.     exit(1);
  80.    }
  81.  DEBUG = 0;                        /* default DEBUG is OFF */
  82.  if(argv[3] != NULL)               /* got a 3rd parm????   */
  83.   {
  84.    if(!strcmp(argv[3],"debug"))    /* .. is it 'debug'???  */
  85.     DEBUG = 1;                     /* .. yep, turn DEBUG 'on' */
  86.   }
  87.  while((sa = fgets(buf,MAXLEN,f1)) != NULL)  /* grab some data */
  88.   {
  89.    howmanyin++;                    /* keep count as to how many I've read */
  90.    sa = sa;                        /* eliminate stupid compiler msg */
  91.    if(DEBUG)                       /* DEBUG 'on'??? */
  92.     {
  93.      isip = strchr(buf,'\n');        /* see if has \n already here */
  94.      if(isip)
  95.       printf("Converting: %s",buf);  /* yep... display input rcd w/o \n */
  96.      else
  97.       printf("Converting: %s\n",buf); /* nope... display input w/ \n */
  98.     }
  99.    isip = buf+20;                        /* see if 'AX.25' ARP   */
  100.    if(!strncmp(isip,"AX.25",5))
  101.     {
  102.      isarp();                            /* go build ARP rcd     */
  103.      writeme();                          /* write it out         */
  104.     }
  105.    if(!strncmp(isip,"Net/Rom",7))        /* see if 'Net/Rom' ARP */
  106.     {
  107.      isarp();                            /* go build arp rcd     */
  108.      writeme();                          /* write it out         */
  109.     }
  110.    isip = buf+22;                        /* see if ip define     */
  111.    if(!strncmp(isip,"Net/Rom",7))        /* see if ip define     */
  112.     {
  113.      isiport = 0;
  114.      isiprec();                         /* go build ip rcd       */
  115.      writeme();                         /* write it out          */
  116.     }
  117.    if(!strncmp(isip,"Port",4))           /* see if ip define     */
  118.     {
  119.      isiport = 1;
  120.      isiprec();                         /* go build ip rcd       */
  121.      writeme();                         /* write it out          */
  122.     }
  123.    isip = strstr(buf,"!");              /* see if locked 'route' */
  124.    if(isip)
  125.     {
  126.      isroute();                         /* go build routes rcd   */
  127.      writeme();                         /* write it out          */
  128.     }
  129.    isip = strstr(buf,"My IP address");  /* see if 'My IP address'*/
  130.    if(isip)
  131.     {
  132.      ismyip();                          /* go build My Ip rcd    */
  133.      writeme();                         /* write it out          */
  134.     }
  135.    isip = strstr(buf,"mode");           /* see if 'mode' display */
  136.    if(isip)
  137.     {
  138.      ismode();                          /* go build 'mode' rcd   */
  139.      writeme();                         /* write it out          */
  140.     }
  141.    isip = strstr(buf,"parm");           /* see if 'parm' display */
  142.    if(isip)
  143.     {
  144.      isparm();                          /* go build 'parm' rcd   */
  145.                                         /* DON'T write this one  */
  146.     }
  147.    memset(buf,' ',80);                  /* clear the input work area */
  148.   }
  149.  fclose(f1);                            /* close my input file  */
  150.  fclose(f2);                            /* close my output file */
  151.  printf("%d records read, %d records converted.\n",howmanyin,howmanyout);
  152. }
  153. void writeme()
  154. {
  155.  if(DEBUG)
  156.   printf("Output Str: %s\n",outbuf);  /* display the output to console */
  157.  strcat(outbuf,"\n");                 /* append the newline */
  158.  fputs(outbuf,f2);                    /* write out the record */
  159.  howmanyout++;                        /* keep count of how many */
  160. }
  161. void ismode()
  162. {
  163.  if((sa = fgets(buf,MAXLEN,f1)) == NULL)  /* grab some data */
  164.   {
  165.    printf("mode rcd failure, missing 1st rcd after 'mode' string\n");
  166.    exit(2);
  167.   }
  168.  memset(outbuf,' ',70);
  169.  outbuf[69] = '\0';
  170.  memcpy(outbuf,"mode ",5);
  171.  incnt = 0;outcnt = 5;
  172.  ptr = strchr(buf,'}');            /* look for fuzzy bracket */
  173.  incnt = ptr - buf;                /* get where it is        */
  174.  incnt = nxtnblk(++incnt);         /* find the mode string   */
  175.  while(buf[incnt] != '\n' && buf[incnt] != '\0')
  176.   outbuf[outcnt++] = buf[incnt++];
  177.  outbuf[outcnt] = '\0';            /* mark end-of-rcd        */
  178. }
  179. void isparm()
  180. {
  181.  if((sa = fgets(buf,MAXLEN,f1)) == NULL)  /* grab some data */
  182.   {
  183.    printf("parm rcd failure, missing 1st rcd after 'parm' string\n");
  184.    exit(2);
  185.   }
  186.  ptr = strchr(buf,'}');            /* look for fuzzy bracket */
  187.  incnt = ptr - buf;                /* get where it is        */
  188.  incnt++;                          /* jump over it           */
  189.  incnt = nxtnblk(++incnt);         /* find the mode string   */
  190.  moveparm();
  191.  if((sa = fgets(buf,MAXLEN,f1)) == NULL)  /* grab some more data */
  192.   {
  193.    printf("parm rcd failure, missing 2nd rcd after 'parm' string\n");
  194.    exit(2);
  195.   }
  196.  incnt = 0;
  197.  moveparm();
  198. }
  199. void moveparm()
  200. {
  201.  while(incnt != -1)
  202.   {
  203.    incnt = nxtnblk(incnt);
  204.    if(incnt != -1)
  205.     {
  206.      memset(outbuf,' ',70);
  207.      outbuf[69] = '\0';
  208.      memcpy(outbuf,"mode  / ",8);
  209.      itoa(cntr++,work,10);
  210.      outcnt = 8;cntr2 = 0;
  211.      while(work[cntr2] != '\0')
  212.       outbuf[outcnt++] = work[cntr2++];
  213.      outcnt++;
  214.      while(buf[incnt] != ' ' && buf[incnt] != '\n' && buf[incnt] != '\0')
  215.       outbuf[outcnt++] = buf[incnt++];
  216.      if(buf[incnt] == '\n' || buf[incnt] == '\0')
  217.       incnt = -1;
  218.      outbuf[outcnt] = '\0';
  219.      writeme();
  220.     }
  221.   }
  222. }
  223. void ismyip()
  224. {
  225.  memset(outbuf,' ',70);
  226.  outbuf[69] = '\0';
  227.  memcpy(outbuf,"ipaddress ",10);
  228.  incnt = 0;outcnt = 10;
  229.  ptr = strchr(buf,'}');            /* look for fuzzy bracket */
  230.  ptr = strchr(ptr,':');             /* .. now look for :      */
  231.  incnt = ptr - buf;                /* get where it is        */
  232.  incnt = nxtnblk(++incnt);         /* find the IP address    */
  233.  while(buf[incnt] != ' ' && buf[incnt] != '\n' && buf[incnt] != '\0')
  234.   outbuf[outcnt++] = buf[incnt++];
  235.  outbuf[outcnt] = '\0';            /* mark end-of-rcd        */
  236. }
  237. void isroute()
  238. {
  239.  memset(outbuf,' ',70);
  240.  outbuf[69] = '\0';
  241.  memcpy(outbuf,"routes ",7);
  242.  incnt = 0;outcnt = 7;
  243.  incnt = nxtnblk(incnt);           /* get 1st non-blank */
  244.  if(buf[incnt] == '>')             /* check for > */
  245.   {                                /* if found bump around it */
  246.    incnt++;                        /* .. bump over it */
  247.    incnt = nxtnblk(incnt);         /* .. and find next non-blank */
  248.   }
  249.  outbuf[outcnt++] = buf[incnt++];  /* move in the port number */
  250.  outcnt++;                         /* need a space here */
  251.  ptr = strchr(buf,':');            /* look for the : seperator */
  252.  if(!ptr)
  253.   {
  254.    printf("Route record missing : delimiter, aborting......\n");
  255.    printf("%s\n",buf);
  256.    exit(4);
  257.   }
  258.  incnt = ++ptr - buf;              /* point to Callsign */
  259.  while(buf[incnt] != ' ')
  260.   outbuf[outcnt++] = buf[incnt++];
  261.  outcnt++;
  262.  outbuf[outcnt++] = '+';          /* move in + sign */
  263.  outcnt++;
  264.  incnt = nxtnblk(incnt);
  265.  while(buf[incnt] != ' ')         /* point to PathQuality */
  266.   outbuf[outcnt++] = buf[incnt++];
  267.  outbuf[outcnt] = '\0';
  268. }
  269. void isarp()
  270. {
  271.  memset(outbuf,' ',70);
  272.  outbuf[69] = '\0';
  273.  memcpy(outbuf,"arp ",4);
  274.  incnt = 0;outcnt = 4;
  275.  for(cnt=0;cnt < 20;cnt++)         /* copy the IP address */
  276.   {
  277.    if(buf[incnt] == ' ') break;
  278.    outbuf[outcnt++] = buf[incnt++];
  279.   }
  280.  outcnt++;
  281.  outbuf[outcnt++] = '+';          /* put in the + sign          */
  282.  outcnt++;                        /* need a space here          */
  283.  incnt = nxtnblk(incnt);          /* find the next non-blank    */
  284.  if(buf[incnt] == 'P')            /* is this a 'Published' ARP? */
  285.   {
  286.    outbuf[outcnt++] = buf[incnt++]; /* .. copy the 'P' string   */
  287.    outcnt++;                      /* need a space here          */
  288.    incnt = nxtnblk(incnt);        /* bump down to hw-type       */
  289.   }
  290.  while(buf[incnt] != ' ')         /* move in the hw-type string */
  291.   outbuf[outcnt++] = buf[incnt++];  /* .. 1 byte at a time      */
  292.  outcnt++;                        /* need a space here          */
  293.  incnt = nxtnblk(incnt);   /* bump down to Callsign and move to output */
  294.  while(buf[incnt] != ' ' && buf[incnt] != NULL && buf[incnt] != '\n')
  295.   outbuf[outcnt++] = buf[incnt++]; /* move in Callsign byte at a time */
  296.  outcnt++;
  297.  incnt = nxtnblk(incnt);           /* bounce down to Mode */
  298.  if(incnt != -1)                   /* if != -1 then a Mode is here */
  299.  while(buf[incnt] != ' ' && buf[incnt] != NULL && buf[incnt] != '\n')
  300.   outbuf[outcnt++] = buf[incnt++]; /* move in Mode (VG/DG) */
  301.  outbuf[outcnt] = '\0';           /* mark the end-of-rcd        */
  302. }
  303. void isiprec()
  304. {
  305.  memset(outbuf,' ',70);
  306.  outbuf[69] = '\0';
  307.  memcpy(outbuf,"iproute ",8);
  308.  incnt = 0;outcnt = 8;
  309.  for(cnt=0;cnt < 20;cnt++)         /* copy the IP address */
  310.   {
  311.    if(buf[incnt] == ' ') break;
  312.    outbuf[outcnt++] = buf[incnt++];
  313.   }
  314.  incnt = nxtnblk(incnt);          /* find the next non-blank */
  315.  if(strncmp(buf+incnt,"32",2))    /* see if Len is 32      */
  316.   {                               /* if not then append it */
  317.    outbuf[outcnt++] = '/';
  318.    while(buf[incnt] != ' ')
  319.     outbuf[outcnt++] = buf[incnt++];
  320.   }
  321.  else
  322.   incnt += 2;
  323. outcnt++;                         /* put a space here */
  324. incnt = nxtnblk(incnt);           /* look for 'Port' string */
  325. if(isiport)
  326.  {
  327.   incnt = nxtblk(incnt);            /* .. jump over it      */
  328.   incnt = nxtnblk(incnt);           /* look for Port number */
  329.  }
  330. outbuf[outcnt++] = '+';           /* move in + and a space */
  331. outbuf[++outcnt] = buf[incnt++];  /* move in Port number */
  332. outcnt += 2;
  333. incnt = nxtblk(incnt);            /* look for Gateway address */
  334. incnt = nxtnblk(incnt);           /* look for Gateway address */
  335. if(incnt != -1)                    /* move in Gateway IP address */
  336.  {
  337.   while(buf[incnt] != ' ' && buf[incnt] != NULL && buf[incnt] != '\n')
  338.    outbuf[outcnt++] = buf[incnt++];
  339.   outcnt++;
  340.  }
  341.  outbuf[outcnt] = '\0';
  342. }
  343. int nxtnblk(int ishere)            /* find the next non-blank byte    */
  344. {
  345.  int work;
  346.  for(work = ishere;work <= ishere + 20;work++)
  347.   {
  348.    if(buf[work] != ' ' && buf[work] != NULL && buf[work] != '\n')
  349.     return work;
  350.   }
  351. return -1;                         /* nohit within 20 bytes, return -1*/
  352. }
  353. int nxtblk(int ishere)             /* find the next blank byte        */
  354. {
  355.  int work;
  356.  for(work = ishere;work < ishere + 20;work++)
  357.   {
  358.    if(buf[work] == ' ' && buf[work] != NULL && buf[work] != '\n')
  359.     return work;
  360.   }
  361. return -1;                         /* nohit within 20 bytes, return -1*/
  362. }
  363.